import os
import logging
import sha
import shutil

class MavenGenerator(object):
    
    M2_HOME = "/Users/brandon/.m2/repository"
    EXT_ARTIFACT = "jar"
    EXT_POM = "pom"
    EXT_SIGNED_ARTIFACT = "jar.sha1"
    EXT_SIGNED_POM = "pom.sha1"
    DIR_TEMP = "/tmp/mavengen"
    
    def __init__(self):
        pass
    
    def generateArtifact(self, artifact, deps):
        basePath = self.generateArtifactPath(artifact, self.M2_HOME)
        jarFile = basePath + self.convertFilename(artifact, self.EXT_ARTIFACT)
        signedJarFile = basePath + self.convertFilename(artifact, self.EXT_SIGNED_ARTIFACT)
        pomFile = basePath + self.convertFilename(artifact, self.EXT_POM)
        signedPomFile = basePath + self.convertFilename(artifact, self.EXT_SIGNED_POM)
        
        pomContent = self.generatePom(artifact, deps)
        
        self.copyFile(artifact.sourceFilePath, jarFile)
        self.writeToFile(pomFile, pomContent)
        self.hashFile(jarFile, signedJarFile)
        self.hashFile(pomFile, signedPomFile)
        
        print basePath
        print jarFile
        print signedJarFile
        print pomFile
        print signedPomFile
        print pomContent 
    
    def copyFile(self, source, destination):
        shutil.copyfile(source, destination)
        pass
    
    def generateDeps(self, deps):
        str = ""
        
        for artifact in deps:
            str = str + "    <dependency> \n"
            str = str + "      <groupId>" + artifact.group + "</groupId> \n"
            str = str + "      <artifactId>" + artifact.name + "</artifactId> \n"
            str = str + "      <version>" + artifact.version + "</version> \n"
            str = str + "    </dependency> \n"
        if str != "":
            str = "  <dependencies> \n" + str
            str = str + "  </dependencies> \n"
        return str
        
    
    def generatePom(self, artifact, deps):
        strDeps = self.generateDeps(deps)
        
        str = "<project> \n"
        str = str + "  <modelVersion>4.0.0</modelVersion> \n"
        str = str + "  <groupId>" + artifact.group + "</groupId> \n"
        str = str + "  <artifactId>" + artifact.name + "</artifactId> \n"
        str = str + "  <version>" + artifact.version + "</version> \n"
        if strDeps != "":
            str = str + strDeps
    
        str = str + "</project>"
        return str
    
    def generateArtifactPath(self, artifact, basePath):
        filePath = basePath
        filePath = filePath + "/" + self.dots2Slashes(artifact.group) 
        filePath = filePath + "/" + artifact.name + "/"
        filePath = filePath + artifact.version + "/"
        if not os.path.exists(filePath):
            os.makedirs(filePath)
        return filePath
    
    def verifyDir(self, path):
        if not os.path.exists(path):
            os.mkdir(path)
        
    def findFileExtension(self, filename):
        return filename.split(".")[1]
    
    def findFilePrefix(self, filename):
        return filename.split(".")[0]
        
    def convertFilename(self, artifact, fileExt):
        artifact.sourceFileName = os.path.basename(artifact.sourceFilePath)
        artifact.filePrefix = self.findFilePrefix(artifact.sourceFileName)
        fileSuffix = self.findFileExtension(artifact.sourceFileName)
        if(fileSuffix != ""):
            artifact.fileSuffix = fileSuffix
        fileName = artifact.name + "-" + artifact.version + "." + fileExt
        return fileName
    
    def dots2Slashes(self, arg):
        return arg.replace(".", "/")
    
    def hashFile(self, source, destination):
        hash = self.getFileHash(source)
        self.writeToFile(destination, hash)
    
    def getFileHash(self, path):
        """Takes a file path and returns a SHA hash of its string"""
        fileStream=open(path,"r").read()
        hash=sha.new(fileStream).hexdigest()
        return hash
    
    def writeToFile(self, path, content):
        file = open(path, "w")
        file.write(content)
        file.close()